package com.chap4;

/**
 * <p>
 * <code>UseThreadLocal</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-03-28 7:34
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
public class UseThreadLocal2 {
    static ThreadLocal<String> threadLocal = new ThreadLocal<>();
    static ThreadLocal<Integer> threadLocal2 = new ThreadLocal<>();

    public static void main(String[] args) {
        UseThreadLocal2 test = new UseThreadLocal2();
        test.startThreadArray();
    }

    public void startThreadArray(){
        Thread[] runs = new Thread[3];
        for (int i = 0; i < runs.length; i++) {
            new TestThread(i).start();
        }
    }

    public static class TestThread extends Thread{
        int id;

        public TestThread(int id) {
            this.id = id;
        }


        @Override
        public void run() {
            String name = Thread.currentThread().getName();
            threadLocal.set("线程_"+id);
            if (id == 2){
                threadLocal2.set(id);
            }
            System.out.println(name+":"+threadLocal.get());
            System.out.println(name+":"+threadLocal2.get());
        }
    }
}
